package com.knife.bbs.action;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.ConfigService;
import com.knife.bbs.object.Config;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;

/**
 * 
 * <p>
 * Title:BBS配置处理类
 * </p>
 * <p>
 * Description: BBS目录的列表处理Action,使用KnifeWeb. <br>
 * 通过继承com.knife.web.tools.AbstractCmdAction,实现简单的命令式Action处理,去除了复杂烦锁的if else语句
 * </p>
 */
public class ConfigAction extends BBSAction {
	private ConfigService configService = ConfigService.getInstance();

	public Page doInit(WebForm form, Module module) {
		return doEdit(form, module);
	}

	/**
	 * 目录列表
	 * 
	 * @param form
	 * @param module
	 * @return 列表Page
	 */
	public Page doList(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		if (!BBSRights.checkAdministrator(user))
			return new Page("popedomError", "norights.htm", "html", "/web/"+skin+"/");
		return doQuery(form, module);
	}

	/**
	 * 修改皮肤操作
	 * 
	 * @param form
	 * @param module
	 * @return 操作成功返回列表Page,失败返回编辑Page
	 */
	public Page doUpdate(WebForm form, Module module) {
		ActiveUser user = getCurrentUser();
		Config dir = configService.getConfig();
		if (dir != null) {
			// 在Action中检测权限
			if (!BBSRights.checkRights(dir, "update", user)) {
				return new Page("popedomError", "norights.htm", "html",
						"/"+skin+"/");
			}
			// 以下执行表单对象VO到PO转换的代码,在验证框架中可以省略
			com.knife.bbs.domain.Config adir = new com.knife.bbs.domain.Config();
			form.toPo(adir);
			configService.update(adir);
		} else {
			form.addResult("msg", "所要修改的目录不存在！");
		}
		return doEdit(form, module);
	}

	/**
	 * 编辑数据,从持久层读取数据显示到VO层
	 * 
	 * @param form
	 * @param module
	 * @return 返回编辑Page
	 */
	public Page doEdit(WebForm form, Module module) {
		ActiveUser user = getCurrentUser();
		Config dir = configService.getConfig();
		if (!BBSRights.checkRights(dir, "edit", user)) {
			return new Page("popedomError", "norights.htm", "html", "/web/"+skin+"/");
		}
		if (dir != null) {
			form.addPo(dir);
		}
		return module.findPage("/"+skin+"/", "edit");
	}

	/**
	 * 目录删除操作,根据主键cid删除相应目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回目录列表页
	 */
	public Page doDel(WebForm form, Module module) {
		ActiveUser user = getCurrentUser();
		Config dir = configService.getConfig();
		if (dir != null) {
			if (!BBSRights.checkRights(dir, "del", user)) {
				return new Page("popedomError", "norights.htm", "html",
						"/"+skin+"/");
			}
			com.knife.bbs.domain.Config adir = new com.knife.bbs.domain.Config();
			configService.del(adir);
			form.addResult("msg", "删除成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 列出所有目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回列表Page
	 */
	public Page doQuery(WebForm form, Module module) {
		form.addResult("list", configService.getConfigs());
		return module.findPage("/"+skin+"/", "list");
	}

	/**
	 * 通过Session取得当前操作用户
	 * 
	 * @return ActiveUser
	 */
	public ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}
}
